package pers.email.config;

import pers.email.exception.MyEmailException;

/**
 * 电子邮件配置
 */
public class EmailConfig {

    private EmailConfig() {
    }

    private EmailConfig(EmailConfig mailConfig) {
        this.host = mailConfig.host;
        this.port = mailConfig.port;
        this.auth = mailConfig.auth;
        this.starttls = mailConfig.starttls;
        this.mailUser = mailConfig.mailUser;
        this.mailPassword = mailConfig.mailPassword;
    }

    private String host;//获取邮箱地址
    private String port;//获取邮箱端口
    private Boolean auth;//设置使用验证
    private Boolean starttls;//使用 STARTTLS安全连接
    private String mailUser;//邮箱用户名
    private String mailPassword;//邮箱密码

    public String getHost() {
        return host;
    }

    public String getPort() {
        return port;
    }

    public Boolean getAuth() {
        return auth;
    }

    public Boolean getStarttls() {
        return starttls;
    }

    public String getMailUser() {
        return mailUser;
    }

    public String getMailPassword() {
        return mailPassword;
    }

    public static class Builder {
        private EmailConfig mailConfig;

        public Builder() {
            mailConfig = new EmailConfig();
        }

        public Builder addHost(String host) {
            mailConfig.host = host;
            return this;
        }

        public Builder addPort(String port) {
            mailConfig.port = port;
            return this;
        }

        public Builder addAuth(Boolean auth) {
            mailConfig.auth = auth;
            return this;
        }

        public Builder addStarttls(Boolean starttls) {
            mailConfig.starttls = starttls;
            return this;
        }

        public Builder addMailUser(String mailUser) {
            mailConfig.mailUser = mailUser;
            return this;
        }

        public Builder addMailPassword(String mailPassword) {
            mailConfig.mailPassword = mailPassword;
            return this;
        }

        /**
         * 构建电子邮件配置
         * @return 配置
         * @throws MyEmailException
         */
        public EmailConfig build() throws MyEmailException {
            if (mailConfig.auth == null) {
                mailConfig.auth = true;
            }
            if (mailConfig.starttls == null) {
                mailConfig.starttls = true;
            }
            if (mailConfig.host == null) {
                throw new MyEmailException("邮箱服务器地址不能为空！");
            }
            if (mailConfig.port == null) {
                throw new MyEmailException("邮箱服务器端口不能为空！");
            }
            if (mailConfig.mailUser == null) {
                throw new MyEmailException("邮箱账户不能为空！");
            }
            if (mailConfig.mailPassword == null) {
                throw new MyEmailException("邮箱密码不能为空！");
            }
            return new EmailConfig(mailConfig);
        }
    }
}
